/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package html_parse;
import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/**
 *
 * @author khanhvv
 */
public class func_My {
  
    String UserName;
    String Password;
    String url;
    public  func_My(String UserName,String Password,String url )
            
    {
        this.Password=Password;
        this.UserName=UserName;
        
        
            this.url="jdbc:mysql://localhost:3307/shopluuniem";
       
        
    }
    public void test()
    {
         Connection conn=null;
        try
        {
            conn=DriverManager.getConnection(url, UserName, Password);
            Statement s = conn.createStatement (); 
            s.executeQuery("select * from sanpham");
      //      s.executeQuery(“SELECT id, name, category FROM animal”); 
            ResultSet rs = s.getResultSet (); 

            while (rs.next ()){ 
         //   int idVal = rs.getInt (“MSSV”);                          // int idVal = rs.getInt (1); 
        //    String nameVal = rs.getString (“HoVaTen”);  // String nameVal = rs.getString (2);     
         //   System.out.println (“MSSV” + idVal + “Ho v ten:”+nameVal); 
                System.out.print("\n "+rs.getString("TenSanPham"));
            } 
            rs.close (); 
            s.close (); 
 
        }
        catch(Exception ex)
        {
            System.out.print(ex.toString());
        }
    }
    public void func_Insert_Product(Product product )
    {
        Connection conn=null;
        try
        {
            conn=DriverManager.getConnection(url, UserName, Password);
            PreparedStatement preS=conn.prepareStatement("Insert into sanpham(id,MaLoaisp,Manhomsp,tensanpham,"+
                    "giasanpham,duongdananh,anhsanpham,giakhuyenmai,thongtinnhanh,thongtinchitiet)"+
                    " values(?,?,?,?,?,?,?,?,?,?);");
            preS.setString(1,String.valueOf(product.getID()));
            preS.setString(2,String.valueOf(product.getMaLoaiSP()));
            preS.setString(3,String.valueOf(product.getMaNhomSP()));
            preS.setString(4,String.valueOf(product.getTenSanPham()));
            preS.setString(5,String.valueOf(product.getGiaSP()));
            preS.setString(6,String.valueOf(product.getDuongDanAnh()));
            preS.setString(7,String.valueOf(product.getAnhSanPham()));
            preS.setString(8,String.valueOf(product.getGiaKM()));
            preS.setString(9,String.valueOf(product.getThongTinNhanh()));
            preS.setString(10,String.valueOf(product.getThongTinChiTiet()));
            preS.executeUpdate();
            conn.close();
        }
        catch(Exception ex)
        {
            System.err.print(ex);
        }
    }
     public void func_Insert_Product(int MaLoaiSP,int ManhomSP,String TenSanPham,String linkChiTiet,String DuongDanAnh,
            String AnhSanPham,String ThongTinNhanh,String ThongTinchiTiet,float GiaSP,float GiaKM )
     {
         Connection conn=null;
        try
        {
            conn=DriverManager.getConnection(url, UserName, Password);
            PreparedStatement preS=conn.prepareStatement("Insert into sanpham(MaLoaiSP,Manhomsp,tensanpham,linkChiTiet,"+
                    "duongdananh,anhsanpham,thongtinnhanh,thongtinchitiet,giasanpham,giakhuyenmai)"+
                    " values(?,?,?,?,?,?,?,?,?,?);");
            preS.setString(1,String.valueOf(MaLoaiSP));
            preS.setString(2,String.valueOf(ManhomSP));
            preS.setString(3,String.valueOf(TenSanPham));
            preS.setString(4,String.valueOf(linkChiTiet));
            preS.setString(9,String.valueOf(GiaSP));
            preS.setString(5,String.valueOf(DuongDanAnh));
            preS.setString(6,String.valueOf(AnhSanPham));
            preS.setString(10,String.valueOf(GiaKM));
            preS.setString(7,String.valueOf(ThongTinNhanh));
            preS.setString(8,String.valueOf(ThongTinchiTiet));
           
            preS.executeUpdate();
            conn.close();
        }
        catch(Exception ex)
        {
            System.err.print(String.valueOf(ex)+"\n");
        }
        
     }
    
     public void func_Insert_ImageSP(int idSanPham,String DuongDanAnh,String TenAnh )
     {
         Connection conn=null;
        try
        {
            conn=DriverManager.getConnection(url, UserName, Password);
            PreparedStatement preS=conn.prepareStatement("Insert into anhchitietsp(IdsanPham,DuongDanAnh,tenAnh) values(?,?,?);");
            preS.setString(1,String.valueOf(idSanPham));
            preS.setString(2,String.valueOf(DuongDanAnh));
            preS.setString(3,String.valueOf(TenAnh));
           
            preS.executeUpdate();
            conn.close();
        }
        catch(Exception ex)
        {
            System.err.print(String.valueOf(ex));
            
        }
        
     }
     public void funf_Insert_Image_SP(String url,int idSanPham) 
     {
         try
         {
            Document doc=Jsoup.connect(url).timeout(10000).get();
            Elements classMoreView=doc.getElementsByClass("more-views");
            for(Element eImg:classMoreView)
            {
                Elements img=eImg.getElementsByTag("a");
                for(Element el:img)
                {
                    func_Insert_ImageSP(idSanPham,el.attr("abs:href"), "");
                    System.out.print("\n-"+el.attr("abs:src")+"   "+idSanPham);
                }
            }
         }
         catch(Exception ex)
         {
             System.err.print(String.valueOf(ex));
         }
         
     }
     public ArrayList<Product> func_GetProduct(int maNhomSP,int maLoaiSP)
     {
         ArrayList<Product> listProduct=new ArrayList<Product>();
         Connection conn=null;
         try
         {
             conn=DriverManager.getConnection(url, UserName, Password);
             Statement s=conn.createStatement();
             s.executeQuery("select * from sanpham");
             ResultSet rs=s.getResultSet();
             while(rs.next())
             {
                 Product product=new Product(rs.getInt("Id"), rs.getInt("MaLoaiSP"), rs.getInt("MaNhomSP"), rs.getString("TenSanPham"),rs.getString("linkChiTiet"), rs.getString("DuongDanAnh"),rs.getString("AnhSanPham"), rs.getString("ThongTinNhanh"),rs.getString("ThongTinChiTiet"), rs.getFloat("GiaSanPham"), rs.getFloat("GiaKhuyenMai"));
                 listProduct.add(product);
              //   System.out.print(product.getDuongDanAnh()   +"\n");
             }
             rs.close();
             conn.close();
             return listProduct;
         }
         catch(Exception ex)
         {
             System.out.print(ex.toString());
         }
         return listProduct;
     }
    
}
